<?php
	include_once '../models/Model.php';
	include_once '../models/Order.php';
	$model=new Model();
	$orderFee=$model->getOrderFee();
	$orderTime=$_POST['orderTime'];
	if(!isset($orderTime))
	{
		echo "<script type='text/javascript'>\n";
		echo "alert('Invalid input time');\n";
		echo "window.location='../view/user/book_room.php'";
		echo "</script>";
	}
	//get room id
	$roomName=$_POST['roomName'];
	$room=$model->searchRoomByName($roomName);
	$roomId=$room->getRoomId();
	//get user id
	session_start();
	$userId=$_SESSION['userId'];
	/*echo $userId;
	die;*/
	foreach($orderTime as $key => $value){
		//get order start time and end time
		$pos = strpos($value , "->");
		$orderStartTime = substr($value, 0, $pos);
		$orderEndTime = substr($value, $pos+2);
		
		$order=new Order($orderId,$roomId, $userId, $orderStartTime,$orderEndTime,1,$orderRealCheckIn, $orderRealCheckOut);
		if($model->addOrder($order)==0)
		{
			$user=$model->searchUserById($userId);
			$username = $user->getUserAccName();
			$date = date("Y-m-d H:i:s");
			$log = $date."---Event---".$username."---Order\n";
			$path="../log/log.txt";
			$file=fopen($path, "a");
			$write=fwrite($file,$log);
			fclose($file);
			echo "<script type='text/javascript'>\n";
			echo "alert('Order successful!You need to pay ".$orderFee."vnd');\n";
			echo "window.location='../view/user/book_room.php'";
			echo "</script>";
		}	
		else
		{
			echo "<script type='text/javascript'>\n";
			echo "alert('Error!Can not order');\n";
			echo "window.location='../view/user/book_room.php'";
			echo "</script>";
		}
	}
	
?>
	